package com.all.play.myapplication.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.Toast;

import com.all.play.myapplication.Bean.GoodsInfo;
import com.all.play.myapplication.R;
import com.all.play.myapplication.adapter.MyPdutAdapter;
import com.all.play.myapplication.utils.CartSorage;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 聪明一只哈 on 2018/4/18.
 * Time: 2018/4/18  16:48
 * <p>
 * <p>
 * <p>
 * 1、	商品管理功能
 * 要求：实现商品的添加、修改、删除、浏览功能，具体实现方式自由发挥。
 */

public class ProductManagerActivity extends AppCompatActivity {
    @BindView(R.id.rv_pudt_manager)
    RecyclerView rvPudtManager;
    @BindView(R.id.bt_add_product)
    Button btAddProduct;
    private int tag=0;
    private MyPdutAdapter myPdutAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pro_manager);
        ButterKnife.bind(this);

        //设置初始化数据
        setInitData();
    }


    /**
     * 点击按钮,添加数据
     */
    @OnClick(R.id.bt_add_product)
    public void onViewClicked() {
        Toast.makeText(ProductManagerActivity.this, "添加数据", Toast.LENGTH_SHORT).show();
        List<GoodsInfo> list = new ArrayList<>();
        GoodsInfo goodsInfo = new GoodsInfo("10" + tag,
               // "http://pics.sc.chinaz.com/files/pic/pic9/201803/zzpic10868.jpg",
                "http://img.zcool.cn/community/0100785859056fa8012060c8c2bbc0.JPG@1280w_1l_2o_100sh.jpg",
                "name" + tag, "2018" + tag);
        list.add(goodsInfo);
        CartSorage.getInstance().addData(goodsInfo);
        tag++;

        setInitData();
    }

    /**
     * 设置初始化数据
     */
    private void setInitData() {

        //从内存中取出,再加载
        List<GoodsInfo> list = CartSorage.getInstance().getAllData();
        Collections.reverse(list);

        myPdutAdapter = new MyPdutAdapter(this, list);
        rvPudtManager.setLayoutManager(new LinearLayoutManager(ProductManagerActivity.this));

        rvPudtManager.setAdapter(myPdutAdapter);


    }


}
